package com.android.monee.view.activity;


import java.util.List;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.monee.R;
import com.android.monee.binding.adapter.BindableButtonObservableAdapter;
import com.android.monee.binding.adapter.BindableEditTextObservableAdapter;
import com.android.monee.binding.adapter.BindableTextViewObservableAdapter;
import com.android.monee.binding.binder.Abstract1WBinder;
import com.android.monee.binding.binder.Simple2WBinder;
import com.android.monee.view.binding.widget.BindableButton;
import com.android.monee.view.binding.widget.BindableEditText;
import com.android.monee.view.binding.widget.BindableTextViewDecorator;
import com.android.monee.view.binding.widget.BindableTextViewDecorator.Text;
import com.android.monee.view.model.NewBudget;
import com.android.monee.view.model.NewBudget.Name;


public class NewBudgetSectionFragment extends Fragment {

	/**
	 * The fragment argument representing the section number for this
	 * fragment.
	 */
	public static final String ARG_SECTION_NUMBER = "section_number";

	private NewBudget newBudget;
	
	public NewBudgetSectionFragment() {
		this.newBudget = new NewBudget();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.page_new_budget,
				container, false);
		
		//TODO : on peut rendre les editText bindable de manière dynamique avec un decorateur ? a tester -> oui, à finir
		//BindableEditText budgetNameET = (BindableEditText) rootView.findViewById(R.id.new_budget_name);	
		EditText budgetNameET = (EditText) rootView.findViewById(R.id.new_budget_name);
		BindableTextViewDecorator deco = new BindableTextViewDecorator(budgetNameET);
		
		BindableEditText totalAmountET = (BindableEditText) rootView.findViewById(R.id.new_budget_total_amount);
		totalAmountET.setRawInputType(Configuration.KEYBOARD_12KEY);
		
		BindableEditText provAmountET = (BindableEditText) rootView.findViewById(R.id.new_budget_prov_amount);
		provAmountET.setRawInputType(Configuration.KEYBOARD_12KEY);
		
		/*new Simple2WBinder<NewBudget, NewBudget, NewBudget.Name, BindableEditText, BindableEditTextObservableAdapter, BindableEditText.Text, String>(
				newBudget, newBudget.new Name(), new BindableEditTextObservableAdapter(
						budgetNameET), budgetNameET.new Text());*/
		new Simple2WBinder<NewBudget, NewBudget, NewBudget.Name, BindableTextViewDecorator, BindableTextViewObservableAdapter, BindableTextViewDecorator.Text, String>(
				newBudget, newBudget.new Name(),
				new BindableTextViewObservableAdapter(deco), deco.new Text());
		//TODO : simplifier les binders
		
		new Simple2WBinder<NewBudget, NewBudget, NewBudget.TotalAmount, BindableEditText, BindableEditTextObservableAdapter, BindableEditText.Text, String>(
				newBudget, newBudget.new TotalAmount(), new BindableEditTextObservableAdapter(
						totalAmountET), totalAmountET.new Text());
		new Simple2WBinder<NewBudget, NewBudget, NewBudget.ProvAmount, BindableEditText, BindableEditTextObservableAdapter, BindableEditText.Text, String>(
				newBudget, newBudget.new ProvAmount(), new BindableEditTextObservableAdapter(
						provAmountET), provAmountET.new Text());
		
		final BindableButton beginButton = (BindableButton) rootView.findViewById(R.id.new_budget_begin);
		beginButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				DialogFragment newFragment = new DatePickerFragment(beginButton, newBudget.getSdf());
			    newFragment.show(getFragmentManager(), "datePicker");			
			}
		});
		new Simple2WBinder<NewBudget, NewBudget, NewBudget.Begin, BindableButton, BindableButtonObservableAdapter, BindableButton.Text, String>(
				newBudget, newBudget.new Begin(), new BindableButtonObservableAdapter(
						beginButton), beginButton.new Text());
		
		final BindableButton termButton = (BindableButton) rootView.findViewById(R.id.new_budget_term);
		termButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				DialogFragment newFragment = new DatePickerFragment(termButton, newBudget.getSdf());
				newFragment.show(getFragmentManager(), "datePicker");			
			}
		});
		new Simple2WBinder<NewBudget, NewBudget, NewBudget.Term, BindableButton, BindableButtonObservableAdapter, BindableButton.Text, String>(
				newBudget, newBudget.new Term(), new BindableButtonObservableAdapter(
						termButton), termButton.new Text());
		
		ListView validationErrorLV = (ListView) rootView.findViewById(R.id.new_budget_validation_errors);
		final ArrayAdapter<String> validationErrorAdapter = new ArrayAdapter<String>(getActivity(), R.layout.validation_error, newBudget.getValidationErrors());
		validationErrorLV.setAdapter(validationErrorAdapter);

		new Abstract1WBinder<NewBudget, NewBudget, NewBudget.ValidationErrors, ArrayAdapter<String>, List<String>>(
				newBudget, newBudget.new ValidationErrors(), validationErrorAdapter) {
					@Override
					public void updateTarget(ArrayAdapter<String> target, List<String> value) {
						target.notifyDataSetChanged();
					}
		};

		
		Button cancelButton = (Button) rootView.findViewById(R.id.new_budget_cancel);
		cancelButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				newBudget.initDefaultValues();				
			}
		});
		Button createButton = (Button) rootView.findViewById(R.id.new_budget_create);
		createButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
						.getSystemService(Activity.INPUT_METHOD_SERVICE);
				inputMethodManager.hideSoftInputFromWindow(getActivity()
						.getCurrentFocus().getWindowToken(), 0);
				
				Toast toast;
				if(newBudget.createInsertNewBudget()){
					toast = Toast.makeText(getActivity(), getResources().getText(R.string.new_budget_create_ok_toast), Toast.LENGTH_SHORT);
				}else{
					toast = Toast.makeText(getActivity(), getResources().getText(R.string.new_budget_create_nok_toast), Toast.LENGTH_SHORT);
				}
				toast.show();
			}
		});
		return rootView;
	}
}

